package com.bawei.shoppingcar.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bawei.shoppingcar.CarActivity;
import com.bawei.shoppingcar.R;
import com.bawei.shoppingcar.entity.CarEntity;
import com.bumptech.glide.Glide;

import java.util.List;

public class CarAdapter extends BaseAdapter {
    private List<CarEntity.DataBean> list;
    private Context context;

    public CarAdapter(List<CarEntity.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View view1 = LayoutInflater.from(context).inflate(R.layout.item_car,null);
        CheckBox cb = view1.findViewById(R.id.cb);
        ImageView iv = view1.findViewById(R.id.iv);
        TextView tvDesc = view1.findViewById(R.id.tv_desc);
        TextView tvPrice = view1.findViewById(R.id.tv_price);
        ImageView ivDelete = view1.findViewById(R.id.iv_delete);//删除
        ImageView ivJia = view1.findViewById(R.id.iv_jia);//➕
        ImageView ivJian = view1.findViewById(R.id.iv_jian);//-
        TextView tvCarNum = view1.findViewById(R.id.tv_carNum);
        //复选框
        cb.setChecked(list.get(i).isCheck());
        //图片
        Glide.with(context).load(list.get(i).getGoods_icon()).into(iv);
        //描述
        tvDesc.setText(list.get(i).getGoods_desc()+"");
        //价格
        tvPrice.setText("原价：¥"+list.get(i).getGoods_price());
        //购物车数量
        tvCarNum.setText(list.get(i).getCar_num()+"");


        //复选框：选中 不选中
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                list.get(i).setCheck(b);
                notifyDataSetChanged();
                CarActivity carActivity = (CarActivity) context;
                carActivity.total();
            }
        });
        //删除
        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.remove(i);
                notifyDataSetChanged();
                CarActivity carActivity = (CarActivity) context;
                carActivity.total();
            }
        });
        //+
        ivJia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int carNum = list.get(i).getCar_num();
                carNum++;
                list.get(i).setCar_num(carNum);
                notifyDataSetChanged();
                CarActivity carActivity = (CarActivity) context;
                carActivity.total();

            }
        });
        //-
        ivJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int carNum = list.get(i).getCar_num();
                carNum--;
                list.get(i).setCar_num(carNum);
                notifyDataSetChanged();
                CarActivity carActivity = (CarActivity) context;
                carActivity.total();
            }
        });

        return view1;
    }
}
